package com.jinlejia.erp.ui.mine.contract;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jinlejia.erp.R;
import com.jinlejia.erp.bean.PactAttachBean;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import java.util.ArrayList;

/**
 * 合同和附件选择
 */

public class ContractSelect extends AlertDialog {

    private Context context;
    ArrayList<PactAttachBean> dataList;
    private SlimAdapter slimAdapter;
    private PactAttachBean checkedBean;

    public ContractSelect(Context c, ArrayList<PactAttachBean> d, PactAttachBean checkedBean) {
        super(c);
        this.context = c;
        dataList = d;
        this.checkedBean = checkedBean;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.dialog_contract_select);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());

        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, (int) (d.getHeight() * 0.4));

        RecyclerView recyclerView = (RecyclerView) window.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setHasFixedSize(true);
        slimAdapter = SlimAdapter.create()
                .register(R.layout.item_contract_select, new SlimInjector<PactAttachBean>() {
                    @Override
                    public void onInject(final PactAttachBean data, IViewInjector injector) {
                        injector.with(R.id.type, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                TextView textView = (TextView) view;
                                if (dataList.indexOf(data) == 0) {
                                    textView.setText("主合同");
                                } else if (data.getType() == 0) {
                                    textView.setText("文本");
                                } else if (data.getType() == 1) {
                                    textView.setText("附件");
                                } else if (data.getType() == 2) {
                                    textView.setText("文本和附件");
                                }
                            }
                        }).with(R.id.name, new IViewInjector.Action<TextView>() {
                            @Override
                            public void action(TextView tv) {
                                tv.setText(data.getPactOrAttachName());
                            }
                        }).visibility(R.id.ismust, data.getIsMust() == 1 && dataList.indexOf(data) != 0 ? View.VISIBLE : View.GONE)
                                .visibility(R.id.icon_word, data.getType() != 1 ? View.VISIBLE : View.GONE)
                                .visibility(R.id.icon_pic, data.getType() != 0 ? View.VISIBLE : View.GONE)
                                .clicked(R.id.layout, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (null != selectListener) {
                                            selectListener.select(data);
                                        }
                                        dismiss();
                                    }
                                }).checked(R.id.checkbox, data == checkedBean ? true : false)
                        ;
                    }
                }).attachTo(recyclerView);

        slimAdapter.updateData(dataList);


    }


    public interface OnSelectListener {
        public void select(PactAttachBean o);
    }

    private OnSelectListener selectListener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.selectListener = listener;
    }
}
